package com.atguigu.guli.service.edu.controller.api;


import com.atguigu.guli.service.base.dto.CourseDto;
import com.atguigu.guli.service.base.dto.UserDto;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.ApiCourseQuery;
import com.atguigu.guli.service.edu.entity.vo.ApiCourseDetailVo;
import com.atguigu.guli.service.edu.service.CourseService;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author leo
 * @since 2021-11-24
 */
@RestController

@RequestMapping("/api/edu/course")
public class ApiCourseController {
    @Autowired
    private CourseService courseService ;
    @Autowired
    private TeacherService teacherService ;

    //远程更新课程销量的接口
    @PutMapping("updateSales/{courseId}")
    public R updateSales(@PathVariable String courseId) {
        Course course = courseService.getById(courseId);
        course.setBuyCount(course.getBuyCount() + 1) ;
        LambdaUpdateWrapper<Course> updateWrapper = new LambdaUpdateWrapper<>() ;
        updateWrapper.set(Course::getBuyCount,course.getBuyCount()) ;
        updateWrapper.eq(Course::getId,courseId) ;
        courseService.updateById(course) ;
        return R.ok() ;
    }
    //支付页面根据课程id查询课程信息
    @GetMapping("getCourseDto/{courseId}")
    public R getCourseDto(@PathVariable String courseId) {
        //为了方便返回值自定义bean的类型可以在 edu和trade服务中都是用，可以将bean定义到公共模块
        Course course = courseService.getById(courseId);
        CourseDto courseDto = new CourseDto();
        BeanUtils.copyProperties(course , courseDto);
        Teacher teacher = teacherService.getById(course.getTeacherId());
        courseDto.setTeacherName(teacher.getName());
        return R.ok().data("item" , courseDto);
    }
    //1.查询讲师对应课程
    @GetMapping("getCoursesByTeacherId/{teacherId}")
    public R getCoursesByTeacherId(@PathVariable String teacherId) {
        List<Course> courses = courseService.getCoursesByTeacherId(teacherId) ;
        return R.ok().data("items",courses) ;
    }

    //2.带条件查询课程
    @GetMapping("list")
    public R list(ApiCourseQuery apiCourseQuery) {
        List<Course> courses = courseService.getCoursesByCondition(apiCourseQuery) ;
        return R.ok().data("items",courses) ;
    }
    //3.用户查询指定id的课程详情
    @GetMapping("getCourseDetailById/{courseId}")
    public R getCourseDetailById(@PathVariable String courseId) {
        ApiCourseDetailVo courseDetailVo = courseService.getCourseDetailById(courseId) ;
        return R.ok().data("item",courseDetailVo) ;
    }

    //4.首页查询热门课程数量（浏览量前8）
    @GetMapping("getHotCourses")
    public R getHotCourses() {
        List<Course> courses = courseService.getHotCourses() ;
        return R.ok().data("items",courses) ;
    }
}

